package com.taomu.app.zongchouhuli.moudle.mine.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;

import java.util.ArrayList;
import java.util.List;

public class SelectBankAdp extends RecyclerView.Adapter<SelectBankAdp.BankHolder>{

    private List<String> mData;

    private Context mContext;

    private LayoutInflater inflater;

    private String selectData;

    public SelectBankAdp(Context mContext, String selectData){
        this.mData = new ArrayList<>();
        this.inflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.selectData = selectData;
    }

    public void setData(List<String> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public BankHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_select_bank,parent,false);
        return new BankHolder(view);
    }

    @Override
    public void onBindViewHolder(BankHolder holder, int position) {
        final String bank = mData.get(position);
        holder.update(bank);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onItemClick(bank);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class BankHolder extends RecyclerView.ViewHolder{

        private TextView tv;

        public BankHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.item_select_bank_tv);
        }

        public void update(String bank){
            tv.setText(bank);
            if(bank.equals(selectData)){
                tv.setTextColor(mContext.getResources().getColor(R.color.base_red));
            }else{
                tv.setTextColor(mContext.getResources().getColor(R.color.text_normal_black));
            }
        }
    }

    private SelectBankListener listener;

    public void setSelectBankListener(SelectBankListener listener){
        this.listener = listener;
    }

    public interface SelectBankListener{
        void onItemClick(String bank);
    }
}
